#!/system/bin/sh

# Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

ros_version=1

if [ -z "${RELEASE_ROOT}" ]; then
	RELEASE_ROOT=/data/local/release
fi

if [ -z "${MESA}" ]; then
	MESA=mesa-panfork
fi

if [ -d ${RELEASE_ROOT}/${MESA} ]; then
	export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${RELEASE_ROOT}/${MESA}/usr/lib
fi
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${RELEASE_ROOT}/lib:${RELEASE_ROOT}/lib64
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${RELEASE_ROOT}/usr/lib:${RELEASE_ROOT}/usr/lib64:${RELEASE_ROOT}/usr/lib/aarch64-linux-ohos

if [ "${NO_PRELOAD_GLEW}" != true ]; then
	export LD_PRELOAD=${LD_PRELOAD}:${RELEASE_ROOT}/usr/lib/libGLEW.so
fi
if [ "${NO_PRELOAD_PYTHON}" != true ]; then
	export LD_PRELOAD=${LD_PRELOAD}:${RELEASE_ROOT}/usr/lib/libpython3.12.so.1.0
fi

export PATH=${PATH}:${RELEASE_ROOT}/bin:${RELEASE_ROOT}/sbin
export PATH=${PATH}:${RELEASE_ROOT}/usr/bin:${RELEASE_ROOT}/usr/sbin

# Common environments
if [ -z "${XDG_RUNTIME_DIR}" ]; then
	export XDG_RUNTIME_DIR=${RELEASE_ROOT}/run
fi
if [ -z "${XDG_CONFIG_HOME}" ]; then
	export XDG_CONFIG_HOME=${RELEASE_ROOT}/etc
fi
if [ -z "${XDG_DATA_HOME}" ]; then
	export XDG_DATA_HOME=${RELEASE_ROOT}/data
fi
if [ -z "${FONTCONFIG_FILE}" ]; then
	export FONTCONFIG_FILE=${RELEASE_ROOT}/etc/fonts/fonts.conf
fi
if [ -z "${XCOMPOSEFILE}" ]; then
	export XCOMPOSEFILE=${RELEASE_ROOT}/usr/share/X11/locale/en_US.UTF-8/Compose
fi
if [ -z "${LIBINPUT_QUIRKS_DIR}" ]; then
	export LIBINPUT_QUIRKS_DIR=${RELEASE_ROOT}/usr/share/libinput
fi

# GPU acceleration related environments
if [ -z "${LIBGL_DRIVERS_PATH}" ]; then
	export LIBGL_DRIVERS_PATH=${RELEASE_ROOT}/${MESA}/usr/lib/dri
fi
if [ -z "${DRIRC_CONFIGDIR}" ]; then
	export DRIRC_CONFIGDIR=${RELEASE_ROOT}/${MESA}/usr/share/drirc.d
fi
if [ -z "${VK_DRIVER_FILES}" ]; then
	export VK_DRIVER_FILES=${RELEASE_ROOT}/${MESA}/usr/share/vulkan/icd.d/panfrost_icd.arm64v8-a.json
fi
if [ -z "${PAN_MESA_DEBUG}" ]; then
	export PAN_MESA_DEBUG=gl3
fi

case ${ros_version} in
	1)
		if [ -z "${ROS_DISTRO}" ]; then
			export ROS_DISTRO=noetic
		fi
		if [ -z "${ROS_HOME}" ]; then
			export ROS_HOME=${RELEASE_ROOT}
		fi
		if [ -z "${ROS_LOG_DIR}" ]; then
			export ROS_LOG_DIR=${RELEASE_ROOT}/log
		fi
		if [ -z "${ROS_PACKAGE_PATH}" ]; then
			export ROS_PACKAGE_PATH=${RELEASE_ROOT}
		fi
		if [ -z "${PYTHONHOME}" ]; then
			export PYTHONHOME=${RELEASE_ROOT}/usr
		fi
		if [ -z "${PYTHONPATH}" ]; then
			export PYTHONPATH=${RELEASE_ROOT}/usr/lib/python3.12/site-packages
		fi
		;;
	2)
		if [ -z "${ROS_DISTRO}" ]; then
			export ROS_DISTRO=jazzy
		fi
		if [ -z "${ROS_VERSION}" ]; then
			export ROS_VERSION=2
		fi
		if [ -z "${ROS_PYTHON_VERSION}" ]; then
			export ROS_PYTHON_VERSION=3
		fi
		if [ -z "${ROS_HOME}" ]; then
			export ROS_HOME=${RELEASE_ROOT}
		fi
		if [ -z "${ROS_LOG_DIR}" ]; then
			export ROS_LOG_DIR=${RELEASE_ROOT}/log
		fi
		if [ -z "${ROS_PACKAGE_PATH}" ]; then
			export ROS_PACKAGE_PATH=${RELEASE_ROOT}
		fi
		if [ -z "${PYTHONHOME}" ]; then
			export PYTHONHOME=${RELEASE_ROOT}/usr
		fi
		if [ -z "${PYTHONPATH}" ]; then
			export PYTHONPATH=${RELEASE_ROOT}/usr/lib/python3.12/site-packages
		fi
		;;
esac

# Helix environments
if [ -z "${HELIX_RUNTIME}" ]; then
	export HELIX_RUNTIME=${RELEASE_ROOT}/etc/helix/runtime
fi

# Debug environments
if [ "${DEBUG}" = true ]; then
	if [ -z "${MESA_DEBUG}" ]; then
		export MESA_DEBUG=1
	fi
	if [ -z "${EGL_LOG_LEVEL}" ]; then
		export EGL_LOG_LEVEL=debug
	fi
	if [ -z "${LIBGL_DEBUG}" ]; then
		export LIBGL_DEBUG=verbose
	fi
	if [ -z "${VK_LOADER_DEBUG}" ]; then
		export VK_LOADER_DEBUG=all
	fi
	if [ -z "${RUST_LOG}" ]; then
		export RUST_LOG="debug,wgpu_hal=debug"
	fi
	if [ -z "${WEZTERM_LOG}" ]; then
		export WEZTERM_LOG="debug"
	fi
	if [ -z "${VGL_VERBOSE}" ]; then
		export VGL_VERBOSE=1
	fi
	#export VGL_DEBUG=1
fi

# Common configs
if [ ! -d /usr/bin ]; then
        mkdir -p /usr/bin
fi

if [ ! -f /usr/bin/env ] || [ ! -L /usr/bin/env ]; then
	ln -sf ${RELEASE_ROOT}/usr/bin/env /usr/bin/env
fi

# Vim configs
if [ ! -d /usr/share/vim ]; then
	mkdir -p /usr/share/
	ln -sf ${RELEASE_ROOT}/usr/share/vim /usr/share/
fi


# X11 configs
if [ ! -d /usr/share/X11 ]; then
        mkdir -p /usr/share
        ln -sf ${RELEASE_ROOT}/usr/share/X11 /usr/share/X11
fi
if [ ! -f /usr/bin/xkbcomp ]; then
        ln -sf ${RELEASE_ROOT}/usr/bin/xkbcomp /usr/bin/xkbcomp
fi

"$@"
